package com.iwe.crm.base.util;

import com.iwe.crm.base.bean.ResultVo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

public class FileUploadUtil {

    public static ResultVo fileUpload(MultipartFile file, HttpServletRequest request){
        ResultVo resultVo = new ResultVo();
        File file1 = new File("d:/upload/");
        if(!file1.exists()){
            file1.mkdirs();
        }

        //获取文件名
        String filename = file.getOriginalFilename();
        //文件重名
        filename = System.currentTimeMillis() + filename;
        try {
            file.transferTo(new File(file1,filename));
            resultVo.setOk(true);
            resultVo.setMessage("上传成功");
            String filePath = request.getContextPath() +"/img/"+ filename;
            resultVo.setT(filePath);
        } catch (IOException e) {
            e.printStackTrace();
            resultVo.setMessage("上传失败");
        }
        return resultVo;
    }
}
